﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.Linq;

namespace DevEx
{
    public class CSLNX
    {
        private static QLVTDataContext GetDc()
        {
            QLVTDataContext dc = new QLVTDataContext();
            return dc;
        }
                
        public IQueryable<tblSLNX> SelectAllRecord(int ID_DPNX)
        {
            QLVTDataContext dc = GetDc();

            var q = from p in dc.tblSLNXes
                    where p.ID_DPNX == ID_DPNX
                    select p;
            return q;
        }

        public tblSLNX SelectRecordByID(int ID_SLNX)
        {
            QLVTDataContext dc = GetDc();

            var q = (from p in dc.tblSLNXes where p.ID_SLNX == ID_SLNX select p).SingleOrDefault();
            return q;
        }

        //Insert
        public void Insert(tblSLNX obj)
        {
            QLVTDataContext dc = GetDc();
            dc.tblSLNXes.InsertOnSubmit(obj);
            dc.SubmitChanges();
        }
        //Delete
        public void Delete(int ID)
        {
            QLVTDataContext dc = GetDc();

            var SLNX = dc.tblSLNXes.SingleOrDefault(p=>p.ID_SLNX == ID);
            dc.tblSLNXes.DeleteOnSubmit(SLNX);
            dc.SubmitChanges();
        }
        //Update
        public void Update(tblSLNX obj)
        {
            QLVTDataContext dc = GetDc();
            dc.tblSLNXes.Attach(obj);
            dc.Refresh(RefreshMode.KeepCurrentValues, obj);
            dc.SubmitChanges(ConflictMode.ContinueOnConflict);
        }

        public void UpdateSLTH(tblSLNX obj)
        {
            QLVTDataContext dc = GetDc();

            var q = (from p in dc.tblSLNXes
                     where p.ID_SLNX == obj.ID_SLNX
                     select p).SingleOrDefault();

            q.SLTH = obj.SLTH;

            dc.SubmitChanges();
        }
        
    }
}
